//
//  OddMoveEvenStillAction.cpp
//  fingeradvanture
//
//  Created by shuaibincheng on 16/5/22.
//
//

#include "OddMoveEvenStillAction.hpp"
#include "TroopForm.hpp"

void OddMoveEvenStillAction::train(const Vector<cocos2d::Node *> &monsters)
{
    trainFirstOne(monsters);
    trainLastOne(monsters);
    trainOthers(monsters);
}

void OddMoveEvenStillAction::trainFirstOne(const Vector<cocos2d::Node *> &monsters)
{
    auto first = monsters.at(0);
    auto leftMove = form->marginLeft();
    auto rightMove = (monsters.at(1)->getPositionX() - first->getPositionX())
        -(monsters.at(1)->getContentSize().width + first->getContentSize().width) / 2 - 5;
    
    auto moveLeft = MoveBy::create(1.0, Vec2(-leftMove, 0.0f));
    auto moveRight = MoveBy::create(1.0, Vec2(rightMove, 0.0f));
    
    auto sequence = Sequence::create(moveLeft, moveLeft->reverse(),
                                     moveRight, moveRight->reverse(), NULL);
    auto downMoveBy = MoveBy::create(15.0f, Vec2(0.0f, -form->getVisibleSize().height
                                                 - form->getTroopSize().height - 20));
    auto repeat = Repeat::create(sequence, 15);
    auto spawn = Spawn::create(repeat,downMoveBy, NULL);
    
    first->runAction(spawn);
}

void OddMoveEvenStillAction::trainLastOne(const Vector<cocos2d::Node *> &monsters)
{
    auto lastIndex = monsters.size() - 1;
    auto last = monsters.at(lastIndex);
    
    auto rightMove = form->marginRight();
    auto leftMove = (last->getPositionX() - monsters.at(lastIndex - 1)->getPositionX()) -
        (last->getContentSize().width + monsters.at(lastIndex - 1)->getContentSize().width) / 2 - 5;
    
    auto moveRight = MoveBy::create(1.0, Vec2(rightMove, 0.0f));
    auto moveLeft = MoveBy::create(1.0f, Vec2(-leftMove, 0.0f));
    auto sequence = Sequence::create(moveRight, moveRight->reverse(),
                                     moveLeft, moveLeft->reverse(), NULL);
    auto downMoveBy = MoveBy::create(15.0f, Vec2(0.0f, -form->getVisibleSize().height
                                                 - form->getTroopSize().height - 20));
    auto repeat = Repeat::create(sequence, 15);
    auto spawn = Spawn::create(repeat,downMoveBy, NULL);
    
    last->runAction(spawn);
}

void OddMoveEvenStillAction::trainOthers(const Vector<cocos2d::Node *> &monsters)
{
    auto begin = monsters.begin() + 2;
    auto end = --monsters.end();
    for(;begin != end; begin += 2)
    {
        auto next = begin + 1;
        auto pre = begin - 1;
        auto rightMove = ((*next)->getPositionX() - (*begin)->getPositionX())
            - ((*next)->getContentSize().width + (*begin)->getContentSize().width) / 2 - 5;
        auto leftMove = ((*begin)->getPositionX() - (*pre)->getPositionX())
            - ((*begin)->getContentSize().width + (*pre)->getContentSize().width) / 2 - 5;
        
        auto moveRight = MoveBy::create(1.0, Vec2(rightMove, 0.0f));
        auto moveLeft = MoveBy::create(1.0f, Vec2(-leftMove, 0.0f));
        auto sequence = Sequence::create(moveRight, moveRight->reverse(),
                                         moveLeft, moveLeft->reverse(), NULL);
        auto downMoveBy = MoveBy::create(15.0f, Vec2(0.0f, -form->getVisibleSize().height
                                                     - form->getTroopSize().height - 20));
        auto repeat = Repeat::create(sequence, 15);
        auto spawn = Spawn::create(repeat,downMoveBy, NULL);
        
        (*begin)->runAction(spawn);
    }
    begin = monsters.begin() + 1;
    end = monsters.end();
    for(; begin != end; begin += 2)
    {
        auto downMoveBy = MoveBy::create(15.0f, Vec2(0.0f, -form->getVisibleSize().height
                                                     - form->getTroopSize().height - 20));
        (*begin)->runAction(downMoveBy);
    }
}

